﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Instantiation style</title>
  <keywords>
    <keyword translate="true">Instance</keyword>
    <keyword translate="true">IoC</keyword>
    <keyword translate="true">Singleton</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Instantiation style</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> in Catel supports instantiation style.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">What does instantiation style mean?</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Some popular IoC containers support several instantiation styles but they could be summarized into </text><text styleclass="Normal" style="font-style:italic;" translate="true">Singleton</text><text styleclass="Normal" translate="true"> or </text><text styleclass="Normal" style="font-style:italic;" translate="true">Transient</text><text styleclass="Normal" style="font-style:normal;" translate="true">. Basically if a type is registered with singleton instantiation style then when the type will be resolved using the IoC container, it will always return the same instance. Otherwise, a new instance is created every time the service is resolved from the IoC container.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" style="font-weight:bold;" translate="true">Example 1: Registering type into Unity Container with singleton instantiation life style</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">UnityContainer</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">container</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">UnityContainer();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">container.RegisterType&lt;IPleaseWaitService,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">PleaseWaitService&gt;(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">new</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ContainerControlledLifetimeManager());</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Registering a type with instantiation style in Catel</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Before Catel 3.3, the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> only supported singleton instantiation style, therefore a sequence of a type resolution always has returned the same instance for the requested type.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Now we added a </text><text styleclass="Normal" style="font-style:italic;" translate="true">boolean</text><text styleclass="Normal" translate="true"> argument to all type registration method family in order to setup if the instantiation style named </text><text styleclass="Normal" style="font-style:italic;" translate="true">singleton</text><text styleclass="Normal" translate="true">. We also care about to the Catel&apos;s user, therefore the default value of this argument is </text><text styleclass="Normal" style="font-style:italic;" translate="true">true, </text><text styleclass="Normal" style="font-style:normal;" translate="true">avoiding unexpected application behaviors provoked by a transient instantiation style.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" style="font-weight:bold;" translate="true">Example 2: Registering type into the ServiceLocator with transient instantiation life style</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Instance.RegisterTypeIfNotYetRegistered&lt;IRegionNavigationJournal, RegionNavigationJournal&gt;(false);</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">External container instantiation style configuration</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> supports synchronization with most of the popular IoC containers, for instance </text><link displaytype="text" defaultstyle="true" type="topiclink" href="IoC_Using_MEF_as_primary_IoC_conta" styleclass="Normal" translate="true">MEF</link><text styleclass="Normal" translate="true">, </text><link displaytype="text" defaultstyle="true" type="topiclink" href="IoC_Using_Unity_as_primary_IoC_con" styleclass="Normal" translate="true">Unity</link><text styleclass="Normal" translate="true">, Windsor and Ninject. This means that you are able to choose between those container and use one of them as primary one. It is also possible to use on the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> of Catel alone. Therefore, if you register an external container into the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> and register a type into this external container or into the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true">, specifying the instantiation style, the instance will be resolved using the right instantiation style.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" style="font-weight:bold;" translate="true">Example 3: Resolving a transient instance of IRegionNavigationJournal type from Unity container registered via Catel ServiceLocator</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">UnityContainer</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">container</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">container();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Instance.RegisterExternalContainer(container);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Instance.RegisterType&lt;IRegionNavigationJournal,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">RegionNavigationJournal&gt;(true, </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">false</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">regionNavigationJournal</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">container.Resolve&lt;IRegionNavigationJournal&gt;();</text></para>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">Be aware when using MEF as primary container. MEF doesn&apos;t support type registration, so it only support singleton instantiation style</text></para>
  </body>
</topic>
